//
//  NetworkAction.m
//  XMGAJ
//
//  Created by Mr.He on 5/27/16.
//  Copyright © 2016 Mr.He. All rights reserved.
//

#import "NetworkAction.h"
#import "NetworkEngine.h"

@implementation NetworkAction {
    NetworkEngine *engine;
}

#pragma mark - Init

- (instancetype)init {
    if (self = [super init]) {
        engine = [[NetworkEngine alloc] init];
    }
    return self;
}

/**
 *  获取默认单例
 */
+ (instancetype)sharedAction {
    static NetworkAction *action;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        action = [[self alloc] init];
    });
    return action;
}

- (void)registerUserWithAccount:(NSString *)account
                       password:(NSString *)password
                          email:(NSString *)email
                        success:(NetworkActionSuccessModelBlock)success
                        failuer:(NetworkActionFailureBlock)failuer {
    
     
    NSString *encode = [UIImage imageChangeBasr64StringWithImage:nil];
    NSDictionary *paramet = @{@"phone":account,
                           @"password":password,
                              @"email":email,
                           @"nickName":account,
                             @"avatar":encode};
    
    [engine POST:@"User/add" parameters:paramet success:^(NSURLSessionDataTask *task, id responseObject) {
        if (success) {
            success(responseObject, @"成功");
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        if (failuer) {
            failuer(error);
        }
    }];
}

- (void)LoginUserWithAccount:(NSString *)account
                       password:(NSString *)password
                        success:(NetworkActionSuccessModelBlock)success
                        failuer:(NetworkActionFailureBlock)failuer {
    
    NSDictionary *paramet = @{@"phone":account, @"password":password};
    
    [engine POST:@"User/login" parameters:paramet success:^(NSURLSessionDataTask *task, id responseObject) {
        if (success) {
            success(responseObject, @"成功");
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        if (failuer) {
            failuer(error);
        }
    }];
}

- (void)changePasswordWithPhone:(NSString *)phone
                         oldPwd:(NSString *)oldPwd
                       password:(NSString *)password
                        success:(NetworkActionSuccessModelBlock)success
                        failuer:(NetworkActionFailureBlock)failuer {
    
    NSDictionary *paramet = @{@"phone":phone, @"oldpassword":oldPwd, @"newpassword":password};
    
    [engine POST:@"User/changePwd" parameters:paramet success:^(NSURLSessionDataTask *task, id responseObject) {
        if (success) {
            success(responseObject, @"成功");
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        if (failuer) {
            failuer(error);
        }
    }];
}

- (void)isHaveUserWithPhone:(NSString *)phone
                      email:(NSString *)email
                        success:(NetworkActionSuccessModelBlock)success
                        failuer:(NetworkActionFailureBlock)failuer {
    
    NSDictionary *paramet = @{@"phone":phone, @"email":email};
    
    [engine POST:@"User/checkUser" parameters:paramet success:^(NSURLSessionDataTask *task, id responseObject) {
        if (success) {
            success(responseObject, @"成功");
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        if (failuer) {
            failuer(error);
        }
    }];
}

- (void)isHaveUserWithPhone:(NSString *)phone
                   password:(NSString *)password
                     userid:(NSString *)userid
                    success:(NetworkActionSuccessModelBlock)success
                    failuer:(NetworkActionFailureBlock)failuer {
    
    NSDictionary *paramet = @{@"phone":phone, @"newpassword":password, @"userid": userid};
    
    [engine POST:@"User/changePwd" parameters:paramet success:^(NSURLSessionDataTask *task, id responseObject) {
        if (success) {
            success(responseObject, @"成功");
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        if (failuer) {
            failuer(error);
        }
    }];
}

- (void)changeUserInfoWithPhone:(NSString *)phone
                       nickname:(NSString *)nickname
                         avatar:(NSString *)avatar
                        success:(NetworkActionSuccessModelBlock)success
                        failuer:(NetworkActionFailureBlock)failuer {
    
    NSDictionary *paramet = @{@"phone":phone, @"nickname":nickname, @"avatar": avatar, @"email":[SMUserInfoModel sharedStorage].email};
    
    [engine POST:@"User/changeUserInfo" parameters:paramet success:^(NSURLSessionDataTask *task, id responseObject) {
        if (success) {
            success(responseObject, @"成功");
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        if (failuer) {
            failuer(error);
        }
    }];
}

- (void)upLoadDataInfoWithPhone:(NSString *)phone
                       jsonData:(NSString *)jsonData
                        success:(NetworkActionSuccessModelBlock)success
                        failuer:(NetworkActionFailureBlock)failuer {
    
    NSDictionary *paramet = @{@"phone":phone, @"json_data":jsonData};
    
    [engine POST:@"User/saveUserMoneyInfo" parameters:paramet success:^(NSURLSessionDataTask *task, id responseObject) {
        if (success) {
            success(responseObject, @"成功");
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        if (failuer) {
            failuer(error);
        }
    }];
}

- (void)downLoadDataInfoWithPhone:(NSString *)phone
                          success:(NetworkActionSuccessModelBlock)success
                          failuer:(NetworkActionFailureBlock)failuer {
    
    NSDictionary *paramet = @{@"phone":phone};
    
    [engine POST:@"User/getUserMoneyInfo" parameters:paramet success:^(NSURLSessionDataTask *task, id responseObject) {
        if (success) {
            success(responseObject, @"成功");
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        if (failuer) {
            failuer(error);
        }
    }];
}

@end
